package com.jacky.path_provider_ex;

import android.util.Log;

import com.dianping.logan.Logan;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Utils {

    private static final String TAG = "PathProvider";

    public static String formatMediaDate(String date){
        String formattedDate = "";
        try {
            Date inputDate = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.getDefault()).parse(date);
            ///utc时间与北京时间相差 8个小时
            inputDate.setTime(inputDate.getTime() + 8 * 3600 * 1000);
            formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(inputDate);
        }
        catch (Exception e){
            Log("formatMediaDate: ", e);
        }
        return formattedDate;
    }

    public static double[] transferVideoGps(String location) {
        if(location == null || location.length() == 0){
            return new double[]{0,0};
        }
        char[] chars = location.toCharArray();
        String latitude = null;
        String longitude = null;
        for (int i = 0; i < chars.length; i++) {
            if ((chars[i] == '+' || chars[i] == '-') && i > 0) {
                latitude = String.copyValueOf(chars, 1, i - 1);
                // length - 1 - (i + 1)
                longitude = String.copyValueOf(chars, i + 1, chars.length - i - 2);
                double dLat = Double.parseDouble(latitude);
                double dLon = Double.parseDouble(longitude);
                return new double[]{dLat, dLon};
            }
        }
        return new double[]{0,0};
    }

    public static boolean isVideo(String suffix) {
        String[] all = new String[]{"flv","mp4","avi","mov","rmvb","webm"};
        for (String s : all) {
            if(s.equalsIgnoreCase(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 转换经度或未读
     * 如112/1,58/1,390971/10000  斜杠代表除号，整体为112/1度，58/1分，390971/10000秒；  度+分/60+秒/3600 为度的形式
     * @param attr 经度或纬度，格式Format is "num1/denom1,num2/denom2,num3/denom3"
     */
    public static double formatGps(String attr){
        if(attr == null || attr.length() == 0){
            return 0.0;
        }
        double dimensionality = 0.0;
        String[] splits = attr.split(",");
        for (int i = 0 ; i < splits.length ; i++){
            String[] nums = splits[i].split("/");
            //得到除数
            double v = Double.parseDouble(nums[0]) / Double.parseDouble(nums[1]);
            //得到的值，除以1、60、3600得到度值，相加
            dimensionality += v / (Math.pow(60, i));
        }
        return dimensionality;
    }

    public static void Log(String message, Throwable e) {
        if(e != null) {
            Log.e(TAG, message, e);
            message += " " + e.getMessage();
        } else {
            Log.d(TAG, message);
        }
        try {
            Logan.w(message, 3);
        } catch (Throwable ignored) {
        }
    }
}
